package com.yijiu.trb;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yijiu.action.BaseAction;
import com.yijiu.network.Request;
import com.yijiu.network.StringUtils;
import com.yijiu.resource.ActionType;
import com.yijiu.resource.C;
import com.yijiu.resource.CacheData;
import com.yijiu.response.BaseResponse;
import com.yijiu.view.DownLoadImage;

public class CutInCheckActivity extends BTitleActivity
{
	private EditText payPwdEditText;

	private String moneyStr;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cut_in_check_layout);
		findViewsInit();
	}

	private void findViewsInit()
	{
		titleView.setText("快捷划入");
		TextView bankPeoPleTextView = (TextView) findViewById(R.id.tv_cut_in_check_name);
		ImageView bankImageView = (ImageView) findViewById(R.id.iv_cut_in_check_bank_icon);
		TextView bankNumTextView = (TextView) findViewById(R.id.tv_cut_in_check_bank_card_num);
		TextView moneyTextView = (TextView) findViewById(R.id.tv_cut_in_check_money);
		payPwdEditText = (EditText) findViewById(R.id.et_cut_in_check_pay_pwd);
		bankPeoPleTextView.setText("开户名：" + CacheData.BANK_PEOPLE_NAME);
		new DownLoadImage(bankImageView).execute(CacheData.BANK_ICON_URL);
		bankNumTextView.setText("银行卡号：" + CacheData.BANK_CARD_NUMBER);
		moneyStr = getIntent().getStringExtra("moneyStr");
		moneyTextView.setText("申请划入金额：￥" + moneyStr);
	}

	public void cutInCheckClick(View v)
	{
		int id = v.getId();
		switch (id)
		{
			case R.id.btn_cut_in_check_submit:
				doCutInOperation();
				break;
			default:
				break;
		}
	}

	private void doCutInOperation()
	{
		String payPwdStr = payPwdEditText.getText().toString();
		if (StringUtils.isNull(payPwdStr))
		{
			app.showMsg("请输入支付密码");
			return;
		}
		String inputStr = "service=deductService&amount=" + moneyStr + "&password=" + payPwdStr;
		encryptBase(inputStr, C.PRIVATE_KEY);
		BaseAction baseAction = new BaseAction(encodedData, encriptData, CacheData.APP_KEY);
		BaseResponse baseResponse = new BaseResponse();
		Request r = new Request(baseAction, baseResponse, ActionType.DO_CUT_IN);
		netManager.excute(r, this, this);
		showProgress("正在划入，请稍候！");
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == RESULT_OK && requestCode == 0)
		{
			Intent intent = getIntent();
			setResult(RESULT_OK, intent);
			finish();
		}
	}

	/**
	 * 操作成功返回的函数
	 */
	@Override
	public void getResult(Request r)
	{
		super.getResult(r);
		int type = r.getType();
		switch (type)
		{
			case ActionType.DO_CUT_IN:
				app.showMsg("划入成功！");
				Intent intent = new Intent(this, OutInDoneFailActivity.class);
				intent.putExtra("flag", "cutInDone");
				startActivityForResult(intent, 0);
				break;
		}
	}

	/* 操作失败返回的函数 */
	@Override
	public void notifyError(int type, int errorCode, String errorMsg)
	{
		super.notifyError(type, errorCode, errorMsg);
		switch (type)
		{
			case ActionType.DO_CUT_IN:
				Intent intent = new Intent(this, OutInDoneFailActivity.class);
				intent.putExtra("flag", "cutInFail");
				intent.putExtra("errorMsg", errorMsg);
				startActivityForResult(intent, 0);
				break;
		}
	}

	/* 重复提交返回的函数 */
	@Override
	public void rePost(Request r)
	{
		super.rePost(r);
	}
}
